package staffs

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/staffs"
	staffsReq "github.com/flipped-aurora/gin-vue-admin/server/model/staffs/request"
	"net/http"
)

type StaffService struct {
}

// CreateStaff 创建staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) CreateStaff(staff *staffs.Staff) (err error) {
	err = global.GVA_DB.Create(staff).Error
	return err
}

// DeleteStaff 删除staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) DeleteStaff(staff staffs.Staff) (err error) {
	err = global.GVA_DB.Delete(&staff).Error
	return err
}

// DeleteStaffByIds 批量删除staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) DeleteStaffByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]staffs.Staff{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateStaff 更新staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) UpdateStaff(staff staffs.Staff) (err error) {
	err = global.GVA_DB.Save(&staff).Error
	return err
}

// GetStaff 根据id获取staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) GetStaff(id uint) (staff staffs.Staff, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&staff).Error
	return
}

// GetStaffInfoList 分页获取staff表记录
// Author [piexlmax](https://github.com/piexlmax)
func (staffService *StaffService) GetStaffInfoList(info staffsReq.StaffSearch) (list []staffs.Staff, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&staffs.Staff{})
	var staffs []staffs.Staff
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&staffs).Error
	return staffs, total, err
}

func (staffService *StaffService) GetStaticData(info staffsReq.StaffSearch) {

	http.Get("http://10.3.50.2:8000/total?OutinPage=?&OutinSize=?")
}
